import { defineStore } from 'pinia'
import { alertmsg } from '@/utils/message.js'
import $ajax from '../api/to8060.js'

//
export const useRoles = defineStore('role', {
  state: () => ({
    /** @type {{ id: number,  rname: string,  remark: string,  nbl: number }[]} */
    roles: [],
    /** @type{ {  text:string ,id:number , isFinished:boolean }}   */
    currentrole: { id: 3, rname: '管理员', remark: '管理网格员的工作', nbl: 1 },
    current_index: 1,
    // 类型将自动推断为 boolean
    dialogFormVisible: false
  }),
  getters: {},
  actions: {
    async hadleNblSwitch(index, row) {
      this.current_index = index
      this.currentrole = row
      const path = '/role/u/byid'
      const res = await $ajax.put(path, this.currentrole)
      if (res.code === 200) {
        alertmsg(res.msg)
      }
    },
    async loaddata() {
      if (this.roles === null || this.roles.length === 0) {
        const path = '/role/s/list/all'
        const res = await $ajax.get(path)
        if (res.code === 200) {
          this.roles = res.result
        }
      }
    }
  }
})
